package com.vogue.supplymall.admin.business.common.task;

import com.vogue.supplymall.admin.business.domain.biz.OpenOrganBiz;
import com.vogue.supplymall.admin.business.domain.biz.TaskProcessorBiz;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.function.Function;

/**
 * The type Task processor.
 * task处理类
 */
@Component
public class TaskProcessor {
    public static final Logger LOGGER_TASKPROCESSOR = LoggerFactory.getLogger(TaskProcessor.class);
    @Resource
    private OpenOrganBiz openOrganBiz;
    @Resource
    private TaskProcessorBiz taskProcessorBiz;

    private final String pattern = "yyyy-MM-dd HH:mm:ss";
    private Function<String, String> currentDate = (String pattern) -> new SimpleDateFormat(pattern).format(new Date());

    /**
     * 串行模式的task
     * check当前的开通的店铺证照有无过期
     */
    @Scheduled(cron = "0 0 1,2,3  * * ? ")
    public void checkShopLicence() {
        LOGGER_TASKPROCESSOR.info("update shop licence auto process start :" + currentDate.apply(pattern));
        try {
            openOrganBiz.checkShopLicence();
            LOGGER_TASKPROCESSOR.info("udate shop licence auto process success" + currentDate.apply(pattern));
        } catch (Exception ex) {
            LOGGER_TASKPROCESSOR.error("update shop licence auto process failed :" + ex);
        } finally {
            LOGGER_TASKPROCESSOR.info("update shop licence auto process end :" + currentDate.apply(pattern));
        }
    }

    /**
     * 管理后台审核通过、未通过证照的定时任务
     */
    @Scheduled(cron = "0 0/10 *  * * ? ")
    public void checkShopLicence2() {
        LOGGER_TASKPROCESSOR.info("update shop licence process start :" + currentDate.apply(pattern));
        try {
            taskProcessorBiz.checkShopLicence2();
            LOGGER_TASKPROCESSOR.info("udate shop licence process success" + currentDate.apply(pattern));
        } catch (Exception ex) {
            LOGGER_TASKPROCESSOR.error("update shop licence process failed :" + ex);
        } finally {
            LOGGER_TASKPROCESSOR.info("update shop licence process end :" + currentDate.apply(pattern));
        }
    }

    /**
     * 停用启用集团
     */
//    @Scheduled(cron = "0 0/1 *  * * ? ")
//    public void onoffOrgan() {
//        LOGGER_TASKPROCESSOR.info("organ opend or stop process start :" + currentDate.apply(pattern));
//        try {
//            taskProcessorBiz.doOnoffOrgan();
//            LOGGER_TASKPROCESSOR.info("organ opend or stop process success" + currentDate.apply(pattern));
//        } catch (Exception ex) {
//            LOGGER_TASKPROCESSOR.error("organ opend or stop process failed :" + ex);
//        } finally {
//            LOGGER_TASKPROCESSOR.info("organ opend or stop process end :" + currentDate.apply(pattern));
//        }
//    }

    /**
     * 包年包月费用计算
     */
//    @Scheduled(cron = "0 0/10 *  * * ? ")
//    public void accountConsumeTask() {
//        LOGGER_TASKPROCESSOR.info("account consume process start :" + currentDate.apply(pattern));
//        try {
//            taskProcessorBiz.accountConsume();
//            LOGGER_TASKPROCESSOR.info("account consume process success" + currentDate.apply(pattern));
//        } catch (Exception ex) {
//            LOGGER_TASKPROCESSOR.error("account consume  process failed :" + ex);
//        } finally {
//            LOGGER_TASKPROCESSOR.info("account consume  process end :" + currentDate.apply(pattern));
//        }
//    }
}
